/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.pay.controller;

import com.bblocks.common.BaseController;
import com.bblocks.pojo.ComUser;
import com.bblocks.util.R;
import com.pig4cloud.pigx.pay.entity.DTO.PayParamsDTO;
import com.pig4cloud.pigx.pay.entity.PayTradeOrder;
import com.pig4cloud.pigx.pay.handler.PayOrderHandler;
import com.pig4cloud.pigx.pay.service.PayChannelService;
import com.pig4cloud.pigx.pay.service.PayTradeOrderService;
import com.pig4cloud.pigx.pay.utils.PayChannelNameEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 支付订单表
 *
 * @author Jin
 * @date 2023-02-28 14:18:17
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/trade")
@Tag(description = "pay=付款（主动付款），collection=收款（收款码收款）", name = "支付订单表管理")
public class PayTradeOrderController extends BaseController {


	private final PayTradeOrderService payTradeOrderService;

	private final PayChannelService channelService;

	private final Map<String, PayOrderHandler> orderHandlerMap;

	@SneakyThrows
	@PostMapping("/pay")
	@Operation(summary = "付款-app", description = "用户付款-app")
	public R pay(@RequestBody PayParamsDTO payInfo) {
		Assert.notNull(payInfo, "参数不能为空！");
		Assert.notNull(payInfo.getChannelId(), "channelId参数不能为空！");
		Assert.notNull(payInfo.getReqNo(), "reqNo参数不能为空！");

		ComUser cuser = this.getCurrentComUser();
		payInfo.setPayType(1);
		Enum channel = PayChannelNameEnum.getChannel(payInfo.getChannelId());
		PayOrderHandler orderHandler = orderHandlerMap.get(channel.name());
		payInfo.setTenantId(cuser!= null? cuser.getTenantId():null);

		PayTradeOrder payTradeOrder = orderHandler.handlePay(payInfo);
		return R.ok(payTradeOrder.getPayInfo());
	}

	@SneakyThrows
	@GetMapping("/wapPay")
	@Operation(summary = "付款-wap", description = "用户付款-wap")
	public void wapPay( PayParamsDTO payInfo) {
		Assert.notNull(payInfo, "参数不能为空！");
		Assert.notNull(payInfo.getChannelId(), "channelId参数不能为空！");
		Assert.notNull(payInfo.getReqNo(), "reqNo参数不能为空！");
		ComUser cuser = this.getCurrentComUser();
		payInfo.setPayType(2);
		Enum channel = PayChannelNameEnum.getChannel(payInfo.getChannelId());
		PayOrderHandler orderHandler = orderHandlerMap.get(channel.name());
		payInfo.setTenantId(cuser!= null? cuser.getTenantId():null);

		PayTradeOrder payTradeOrder = orderHandler.handlePay(payInfo);
	}

}
